{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "title: 🚑毕设-颜色建模-Posit\n",
    "password: \"\"\n",
    "tags:\n",
    "  - Math\n",
    "  - Color\n",
    "katex: true\n",
    "comments: true\n",
    "aside: true\n",
    "date: 2023-05-10 17:35:04\n",
    "cover: https://pan.weidows.tech/d/local/blog/ZMd92o.jpg\n",
    "top_img:\n",
    "---\n",
    "\n",
    "<!--\n",
    " * @?: *********************************************************************\n",
    " * @Author: Weidows\n",
    " * @LastEditors: Weidows\n",
    " * @LastEditTime: 2022-04-20 23:11:24\n",
    " * @FilePath: \\Blog-private\\scaffolds\\post.md\n",
    " * @Description:\n",
    " * @!: *********************************************************************\n",
    "-->\n",
    "\n",
    "## 序\n",
    "\n",
    "### 23.5.10\n",
    "\n",
    "毕业寄了, 导师也开催了, 开始瞎搞. 会按时间版本记录修理过程\n",
    "\n",
    "<a>![分割线](https://cdn.jsdelivr.net/gh/Weidows/Weidows/image/divider.png)</a>\n",
    "\n",
    "\n",
    "## softposit\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-05-16T11:29:29.495544Z",
     "end_time": "2023-05-16T11:29:29.691663Z"
    },
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.1875\n",
      "\u001B[1;37;41m0\u001B[0m\u001B[1;30;43m0001\u001B[0m\u001B[1;37;40m100\u001B[0m\n",
      "-7.51171875\n"
     ]
    }
   ],
   "source": [
    "from softposit import *\n",
    "\n",
    "#Initialise to zero\n",
    "pA = posit32()\n",
    "\n",
    "#Initialise the bits\n",
    "pB = posit16(bits=0x98FA)\n",
    "\n",
    "#Initialise based on real values\n",
    "pC = posit8(0.1875)\n",
    "print(pC)\n",
    "\n",
    "#To view the bits (formatted) -> works only in ipython\n",
    "pC.toBinaryFormatted()\n",
    "\n",
    "#To view the bits without formatting\n",
    "pB.toBinary()\n",
    "\n",
    "#To view real value\n",
    "print(pB)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-05-11T17:08:15.988614Z",
     "start_time": "2023-05-11T17:08:15.963696Z"
    },
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.19921875\n"
     ]
    }
   ],
   "source": [
    "import softposit as sp\n",
    "\n",
    "#Initialise a 16-bit posit\n",
    "a = sp.posit16(2.6)\n",
    "b = a + 2.1\n",
    "a *= 2\n",
    "c = b / a\n",
    "\n",
    "# fused-multiply-add => c + (0.2* 2.3)\n",
    "c.fma(0.2, 2.3)\n",
    "\n",
    "result = c.sqrt()\n",
    "\n",
    "# convert a 16-bit posit to 8-bit posit\n",
    "p8 = c.toPosit8()\n",
    "\n",
    "#quire - initialise to zero when declared\n",
    "q = sp.quire16()\n",
    "\n",
    "#fused multiply subtract -> q + (6.2*1.2)\n",
    "q.qms(6.2, 1.2)\n",
    "\n",
    "#convert quire to posit\n",
    "c = q.toPosit()\n",
    "\n",
    "#to clear quire to zero\n",
    "q.clr()\n",
    "\n",
    "#print\n",
    "print(a)\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<a>![分割线](https://cdn.jsdelivr.net/gh/Weidows/Weidows/image/divider.png)</a>\n",
    "\n",
    "## 混色\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-05-24T18:00:53.864858300Z",
     "start_time": "2023-05-24T18:00:53.638977800Z"
    }
   },
   "outputs": [],
   "source": [
    "import mixbox\n",
    "\n",
    "rgb1 = (255, 0, 0)  # red\n",
    "rgb2 = (0, 255, 0)  # green\n",
    "rgb3 = (0, 0, 255)  # blue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(144, 113, 7)\n",
      "(37, 96, 30)\n"
     ]
    }
   ],
   "source": [
    "mixed_color = mixbox.lerp((255,0,0), (0,255,0), 0.5)\n",
    "print(mixed_color)\n",
    "\n",
    "mixed_color = mixbox.lerp((0,128,0), (64,64,64), 0.5)\n",
    "print(mixed_color)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-05-24T18:00:56.464367800Z",
     "start_time": "2023-05-24T18:00:56.405843100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-05-15T16:05:33.747006Z",
     "start_time": "2023-05-15T16:05:33.739353Z"
    },
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(95, 156, 5)\n",
      "(31, 220, 2)\n"
     ]
    }
   ],
   "source": [
    "mixed_color = rgb1\n",
    "for i in range(10):\n",
    "    mixed_color = mixbox.lerp(mixed_color, rgb2, 0.1)\n",
    "print(mixed_color)\n",
    "\n",
    "for i in range(10):\n",
    "    mixed_color = mixbox.lerp(mixed_color, rgb2, 0.1)\n",
    "print(mixed_color)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-05-15T16:05:40.242871Z",
     "start_time": "2023-05-15T16:05:40.218488Z"
    },
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(255, 255, 233)\n"
     ]
    }
   ],
   "source": [
    "z1 = mixbox.rgb_to_latent(rgb1)\n",
    "z2 = mixbox.rgb_to_latent(rgb2)\n",
    "z3 = mixbox.rgb_to_latent(rgb3)\n",
    "\n",
    "z_mix = [0] * mixbox.LATENT_SIZE\n",
    "\n",
    "for i in range(len(z_mix)):  # mix together:\n",
    "    z_mix[i] = (0.5 * z1[i] +  #   30% of rgb1\n",
    "                0.5 * z2[i] +  #   60% of rgb2\n",
    "                0.5 * z3[i])  #   10% of rgb3\n",
    "print(mixbox.latent_to_rgb(z_mix))\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "- 通过上面的结果看\n",
    "\n",
    "  1.  对于色光来说, 混色的色值符合自然\n",
    "  2.  对于物理颜料来说, 越混越亮是不可能的, 显然还需要另一个参数'亮度'来指示\n",
    "\n",
    "这个项目是比较符合自然颜料的混色算法的, 但并不适用自然情况的亮度衰减\n",
    "\n",
    "考虑纯色颜料, 不考虑用量情况下, 比如红+绿再怎么混色也没法接近黑\n",
    "\n",
    "混色是 min/max 算法, 也就是无论什么颜料+黑色都会无限变黑, +白色都会无限变淡\n",
    "\n",
    "自然物体不反光则是(0,0,0), 完全反光则是(255,255,255)\n",
    "\n",
    "---\n",
    "\n",
    "### 混色亮度衰减\n",
    "\n",
    "考虑 (10,10,10)的黑色笔 + (100,0,0)的红色笔会出什么颜色?\n",
    "\n",
    "<div style=\"color: rgba(60, 3, 7, 1)\">这个颜色</div>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-05-15T16:16:04.746821Z",
     "start_time": "2023-05-15T16:16:04.669806Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(60, 3, 7)\n"
     ]
    }
   ],
   "source": [
    "mixed_color = mixbox.lerp((10, 10, 10), (100, 0, 0), 0.5)\n",
    "print(mixed_color)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-05-15T16:29:27.850600Z",
     "end_time": "2023-05-15T16:29:27.862133Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "0.005126233499935922"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "30/255/3 * 100/255/3\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "data": {
      "text/plain": "0.1111111111111111"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# (255,0,0)亮度Y\n",
    "\n",
    "# 先RGB转XYZ\n",
    "# xyz(41.2%, 21.3%, 1.9%)\n",
    "\n",
    "41.2=Y*0.683/0.313\n",
    "Y*(1-0.157-0.021)/0.021=0"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-05-15T16:30:19.728826Z",
     "end_time": "2023-05-15T16:30:19.781241Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 借物表\n",
    "\n",
    "<a name='cite_note-1' href='#cite_ref-1'>[1]</a>: [Posit: Part 1](https://posithub.org/docs/PositTutorial_Part1.html)\n"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
